package com.cardbaobao.cardbabyclient.ui.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.buried.Google;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.ui.fragment.base.BaseFragment;
import com.cardbaobao.cardbabyclient.ui.fragment.base.MyFragmentPagerAdapter;
import com.cardbaobao.cardbabyclient.ui.fragment.collect.CollectCreditFragment;
import com.cardbaobao.cardbabyclient.ui.fragment.collect.CollectLoanFragment;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.UmengEventUtils;
import com.cardbaobao.cardbabyclient.widget.TitleBar;
import com.gyf.barlibrary.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/12/29.
 * 我的收藏
 */
public class MyCollectActivity extends BaseActivity {
    private static final int ITEM_CREDIT=0;//收藏的信用卡
    private static final int ITEM_LOAN=1;//收藏的贷款

    private static final int COLOR_NORMAL=0xff999999;
    private static final int COLOR_SELECTED=0xff222222;

    @BindView(R.id.title_bar)
    TitleBar titleBar;

    @BindView(R.id.tv_type_loan)
    TextView tvTypeLoan;//贷款
    @BindView(R.id.tv_type_credit_card)
    TextView tvTypeCreditCard;//信用卡

    /**
     * ViewPager容器
     */
    @BindView(R.id.id_vp_container)
    ViewPager mViewPager;

    private List<BaseFragment> mBaseFragments;
    /**
     * ViewPager滑动适配器
     */
    private MyFragmentPagerAdapter mAdapter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        ImmersionBar.with(this).statusBarDarkFont(true,Constant.STATUS_BAR_FONT_ALPHA).init();
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initViewPager();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_collect;
    }

    boolean onResumed;
    @Override
    protected void onResume() {
        super.onResume();
        onResumed=true;
        if(mViewPager!=null){
            int position=mViewPager.getCurrentItem();
            countUmengPage(position);
        }
        GoogleAnalyticsUtil.setScreenName(Google.MY_COLLECT);
    }

    @Override
    protected void onPause() {
        super.onPause();
        onResumed=false;
    }

    /**
     *  初始化ViewPager
     */
    private void initViewPager() {
        if (mBaseFragments == null) {
            mBaseFragments = getFragments();
        }
        mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), mBaseFragments);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(ITEM_CREDIT);
        mViewPager.addOnPageChangeListener(new MyPageChangeListener());

    }

    /**
     *  获取Fragment
     */
    private List<BaseFragment> getFragments() {
        List<BaseFragment> mFragments = new ArrayList<>();
        CollectCreditFragment collectCreditFragment=new CollectCreditFragment();
        collectCreditFragment.setBuriedInfo(mPrePageId);
        mFragments.add(collectCreditFragment);

        CollectLoanFragment collectLoanFragment=new CollectLoanFragment();
        collectLoanFragment.setBuriedInfo(mPrePageId);
        mFragments.add(collectLoanFragment);

        return mFragments;
    }



    /**
     *  ViewPager滑动监听
     */
    private class MyPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            toggleSelectText(position);
            //切换标题
            if(position == ITEM_CREDIT){
                //信用卡
                mCurrentPageId=Self.Page.app_collected_credit_card_page;
            }else{
                //贷款
                mCurrentPageId=Self.Page.app_collected_loan_page;
            }
            if(onResumed){
                countUmengPage(position);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }

    @OnClick( {R.id.tv_type_loan,R.id.tv_type_credit_card})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.tv_type_loan:
                toggleSelectText(ITEM_LOAN);
                break;
            case R.id.tv_type_credit_card:
                toggleSelectText(ITEM_CREDIT);
                break;
        }
    }

    private void toggleSelectText(int selectedPosition){
        mViewPager.setCurrentItem(selectedPosition);
        if(selectedPosition==ITEM_CREDIT){
            tvTypeLoan.setTextColor(COLOR_NORMAL);
            tvTypeCreditCard.setTextColor(COLOR_SELECTED);
        }else{
            tvTypeLoan.setTextColor(COLOR_SELECTED);
            tvTypeCreditCard.setTextColor(COLOR_NORMAL);
        }
    }

    private void countUmengPage(int position){
        if(position==ITEM_LOAN){
            //贷款 event_0x01c
            UmengEventUtils.pageEvent(mContext,getString(R.string.event_0x01c));
        }else{
            //信用卡 event_0x01b
            UmengEventUtils.pageEvent(mContext,getString(R.string.event_0x01b));
        }
    }
}
